package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.entity.SysNhorgan;
import com.medical.lsq.entity.SysPolicy;
import com.medical.lsq.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;
    /**
     * 获取慢病政策信息列表，包含模糊查询，分页
     * @param currentPage
     * @param limit
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getPolicyList")
    public String getPolicyList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String policyYear = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysPolicy> pageInfo = policyService.getPolicyList(currentPage, limit, policyYear);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", policyYear);
        return "/policy/policy_list";
    }

    @RequestMapping("/openAdd")
    public String openadd() {return "/policy/policy_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id,Model model) {
        SysPolicy policy = policyService.getPolicy(id);

        model.addAttribute("policy", policy);
        return "/policy/policy_edit";
    }
    @RequestMapping("/add")
    public String add(SysPolicy sysPolicy,HttpServletRequest request, Model model){
        policyService.add(sysPolicy);
        return getPolicyList(1, 10, request, model);
    }

    @RequestMapping("edit")
    public String edit(SysPolicy policy, HttpServletRequest request, Model model) {
        policyService.edit(policy);
        return getPolicyList(1, 10, request, model);
    }

    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        policyService.delete(id);
        return getPolicyList(1, 10, request,model);
    }

}
